package rabbitmq.topic;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import rabbitmq.Connect;
import rabbitmq.constant.Constants;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Productor {
    public static void main(String[] args) throws IOException, TimeoutException {
        Connection connection = Connect.createConnection();
        // 创建信道
        Channel channel = connection.createChannel();
        // 声明
        channel.exchangeDeclare(Constants.TOPIC_EXCHANGE, BuiltinExchangeType.TOPIC, false);
        channel.queueDeclare(Constants.TOPIC_QUEUE1, false, false, true, null);
        channel.queueDeclare(Constants.TOPIC_QUEUE2, false, false, true, null);
        // 绑定
        channel.queueBind(Constants.TOPIC_QUEUE1, Constants.TOPIC_EXCHANGE, "*.a.*");
        channel.queueBind(Constants.TOPIC_QUEUE2, Constants.TOPIC_EXCHANGE, "*.*.c");
        channel.queueBind(Constants.TOPIC_QUEUE2, Constants.TOPIC_EXCHANGE, "c.#");
        // 发送
        String msg1 = "hi rabbitMQ, my routingKey is haha.a.c";
        channel.basicPublish(Constants.TOPIC_EXCHANGE, "haha.a.c", null, msg1.getBytes()); // Q1 Q2
        String msg2 = "hi rabbitMQ, my routingKey is hihao.topic.c";
        channel.basicPublish(Constants.TOPIC_EXCHANGE, "hihao.topic.c", null, msg2.getBytes()); // Q2
        String msg3 = "hi rabbitMQ, my routingKey is c.a.hehe.wo";
        channel.basicPublish(Constants.TOPIC_EXCHANGE, "c.a.hehe.wo", null, msg3.getBytes()); // Q2
        // 释放资源
        channel.close();
        connection.close();
    }
}
